/**********************************************************************/
/*
   Title: individual_susenas.do
   Authors: Robbie Dulin, Clotaire Boyer
   Description: Runs indiv level analysis on SUSENAS 19
*/
/**********************************************************************/

cap log close
local prefix: display %tdCYND td(`c(current_date)')
log using "$log/`prefix'_indiv_SUS", replace text

clear
set more off

set seed 11022022

global finance_data "$cleaned/finance"

// qui cd "$output/ster_files/20210226_finance"

u "$finance_data/susenas_mar19_finance_ind_lasso.dta", clear

* rename to shorten
rename online_banking ebank
rename online_buy     ebuy
rename online_sell    esell
rename cell_phone*    cell*
rename internet_use*  internet*
rename smallbui_sus19_all bus
rename smallbui_sus19_all_mar18 bus_mar18

* subsamples for heterogeneity analysis
local men     = "& female == 0"
local women   = "& female == 1"
local old     = "& under30 == 0"
local young   = "& under30 == 1"
local lowedu  = "& high_edu == 0"
local hiedu   = "& high_edu == 1"
local ret = "& bus_ret == 1"
local food = "& bus_food == 1"
local manuf = "& bus_manuf == 1"
local other = "& bus_other == 1"
local trans = "& bus_trans == 1"
local agr = "& bus_agr == 1"
local notagr = "& bus_nagr = 1"

* village subsamples
local 0    = "& num_agents_mar18 == 0"
local 2    = "& at_least_2_mar18 == 0"
local full = ""

local anysex = "&  1 == 1"


* regression loop
foreach desas in 0 full {


  foreach var of varlist savings  {

    * get baseline control if it exists
    local mar18 = ""
    cap confirm variable `var'_mar18
    if _rc == 0 {
      local mar18 = "`var'_mar18"
    }

    foreach pmt in pmt30 above all {

      di "Var: `var' | PMT: `pmt' | Desas: `desas'"

      foreach sample in  men women anysex {

        di "Var: `var' | PMT: `pmt' | Sample: `sample' | Desas: `desas'"

        * DL regression
        quietly {
          eststo `var'`pmt'`sample'`desas': pdslasso `var' treated (`mar18' mar16* mar17* mar18* *podes *udb i.finalstratum) ///
            if `pmt' == 1 & adult == 1 ``sample'' ``desas'', cluster(KABU) aset(i.finalstratum `mar18')
            summ `var' if e(sample) == 1 & treated == 0
            estadd scalar control_mean = r(mean)
            estadd scalar obs = e(N)
            estadd local stratum = "Yes"
            estadd local lasso = "Yes"
        }

        * RI p-val
        randinference treated using "$importable/resample1000.dta", ///
          reg(reg `var' treated `e(xselected)' if `pmt' == 1 & adult == 1 ``sample'' ``desas'', vce(cluster KABU)) ///
          reps($RI) mergevar(namakabupaten) mergetype(m:1)
        matrix define pvalues = (`r(RIpval)')
        matrix colnames pvalues = treated
        est restore `var'`pmt'`sample'`desas'
        estadd matrix pvalues
      }
    }
  }
}

local prefix: display %tdCYND td(`c(current_date)')
cap erase "$output/ster_files/20210226_finance/`prefix'_mar19_ind_sav" // clear .ster if it already exists
qui estimates dir
foreach e in `r(names)' {
    quietly estimates restore `e'
    estimates title: `e'
    quietly estimates save "$output/ster_files/20210226_finance/`prefix'_mar19_ind_sav", append
}

log out
